package com.app.shortvideo.net.base

import android.content.Context
import androidx.annotation.StringRes
import com.blankj.utilcode.util.ResourceUtils

/**
 * 响应状态码
 * 不同业务即使有类似的场景错误码也不要相同
 * 比如说：如果昵称长度不合法，和密码长度不合法，按道理说可以共用一个错误码，
 * 但客户端是用错误码取对应的文案，所以不能用相同的错误码
 */
object ResponseCode {
    /**
     * 请求成功
     */
    const val REQUEST_SUCCESS = "1"

    /**
     * NetWork error.
     */
    const val NETWORK_ERROR = "1000"

    /**
     * Token expired
     */
    const val INVALID_TOKEN = "-10001"

    /**
     * Confirm提示信息
     */
    const val CODE_CONFIRM_TIPS = "-10002"

    /**
     * 新设备登录
     */
    const val SIGN_NEW_DEVICE = "-10000"

    /**
     * 显示后台返回的错误信息
     */
    const val CODE_10000 = "100000"
    /**
     * 用户被封禁
     */
    const val CODE_100005 = "100005"
    /**
     * 强制更新
     */
    const val CODE_900000 = "900000"
    /**
     * 未登录
     */
    const val CODE_INVALID_AUTO = "-1"

    /**
     * 重新获取FIT权限
     */
    const val CODE_FIT_AUTH = "-666"

    /**
     * 重复上传
     */
    const val REPEAT_UPLOAD = "141"

    fun isRequestSuccess(code: String?): Boolean {
        return REQUEST_SUCCESS == code
    }

    fun isInValidToken(code: String?): Boolean {
        return INVALID_TOKEN == code
    }

    @StringRes
    fun getStringResIdByCode(code: String?): Int {
        @StringRes var resId: Int = ResourceUtils.getStringIdByName("response_error_code_$code")
        if (resId == 0) {
            resId = com.app.library.R.string.error_no_network
        }
        return resId
    }

    fun getStringByCode(context: Context, code: String?): String? {
        @StringRes val resId = getStringResIdByCode(code)
        return context.getString(resId)
    }

    fun isUploadRepeat(errorCode: String): Boolean {
        return REPEAT_UPLOAD == errorCode
    }

    fun isMastUpdate(code: String?): Boolean {
        return CODE_900000 == code
    }
}